package com.dreamstations.changshads.activity;

import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.entity.Article;
import com.dreamstations.changshads.entity.BaseBean;
import com.dreamstations.changshads.util.DSParser;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.L;
import com.dreamstations.changshads.util.RequestType;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

public class PicTopicActivity extends AppCompatActivity {
    private final static String TAG = PicTopicActivity.class.getName();
    private final static boolean ISDEV = false;

    private Toolbar toolbar;
    private ViewPager viewPager;
    private TextView textView;
    private List<PhotoView> photoViews;// 滑动的图片集合
//    private ScrollView scrollView_Text;
    private HomePageADAdapter adapter;

    private int originIndex =0;

    // 异步加载图片
    private Picasso picasso;

    private String title;
//    private String content;
//    private List<String> articleUrls;
//    private List<String> introStrs;
    private List<Article> articleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pictopic);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        viewPager = (ViewPager) findViewById(R.id.viewPage);
        textView = (TextView) findViewById(R.id.content_detail);
//        scrollView_Text = (ScrollView) findViewById(R.id.scrollview_text);

        picasso = Picasso.with(this);
//        articleUrls = new ArrayList<>();
//        introStrs = new ArrayList<>();
        photoViews = new ArrayList<>();
        adapter = new HomePageADAdapter();
//        for (int i = 0; i < 6; i++) {
//            ImageView imageView = new ImageView(this);
//            // 异步加载图片
//            picasso.load("http://i0.hdslb.com//group1//M00//B7//62//oYYBAFdUPAeAJtqEAAD-FhGdYqs989.jpg").into(imageView);
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            photoViews.add(imageView);
//        }
        if (ISDEV) {
            loadMockData();
        } else {
            loadArticle();
        }
        setTitle(title);
        viewPager.setAdapter(adapter);
        adapter.setPhotoData();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                textView.setText(articleList.get(position).getDoc());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //虽然不生效，不过好像也无伤大雅就是了
//        viewPager.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (toolbar.getVisibility() == View.GONE) {
//                    toolbar.setVisibility(View.VISIBLE);
//                    scrollView_Text.setVisibility(View.VISIBLE);
//                } else {
//                    toolbar.setVisibility(View.GONE);
//                    scrollView_Text.setVisibility(View.GONE);
//                }
//            }
//        });
        textView.setText(articleList.get(originIndex).getDoc());
    }

    /**
     * load的目的是获取两个list的信息
     */
    private void loadMockData() {
//        List<Article> articleList = new ArrayList<>();
//        for (int i = 0; i < 3; i++) {
//            Article article = new Article("intro", i+"　　习近平指出，我们党在中国这样一个有着13亿人口的大国执政，面对着十分复杂的国内外环境，肩负着繁重的执政使命，如果缺乏理论思维的有力支撑，是难以战胜各种风险和困难的，也是难以不断前进的。党的各级领导干部特别是高级干部，要原原本本学习和研读经典著作，努力把马克思主义哲学作为自己的看家本领，坚定理想信念，坚持正确政治方向，提高战略思维能力、综合决策能力、驾驭全局能力，团结带领人民不断书写改革开放历史新篇章。(新华社北京12月4日电)");
//            articleList.add(article);
//            introStrs.add(article.getContent());
//            Article article2 = new Article("path", "http://i1.buimg.com/3ee901bfed3694d3s.png");
//            articleList.add(article2);
//            articleUrls.add(article2.getContent());
//        }
//        title = "毛泽东的抗日国际统一战线思想及其历史贡献";
//        adapter.setPhotoData();
    }

    /**
     * load的目的是获取两个list的信息
     */
    private void loadArticle() {
        try {
            articleList = (List<Article>) getIntent().getSerializableExtra("data");
            originIndex =getIntent().getIntExtra("index",0);
            title=getIntent().getStringExtra("title");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (articleList == null) {
            Toast.makeText(getApplicationContext(), "=>loadArticle:" + "没有传进数据", Toast.LENGTH_SHORT).show();
            return;
        }
//        title = o.getTitle();
//        cover = o.getCover();
//        final String content = data.getContent();
//        //解析特供数据并且填充内容
//        if (content == null || content.equals("")) {
//            Toast.makeText(getApplicationContext(), "=>loadArticle:" + "无数据", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        new Handler().post(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    List<Article> result = (List<Article>) DSParser.parse(RequestType.PARSE_ARTICLE, content);
//                    adapter.setItemDatas(result);
//                    for (Article a : result) {
//                        articleUrls.add(a.getUrl());
//                        articleDocs.add(a.getDoc());
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    Toast.makeText(getApplicationContext(), "加载失败！", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
    }


    @Override
    protected void onPause() {
        picasso.cancelTag(getApplicationContext());
        super.onPause();
    }

    class HomePageADAdapter extends PagerAdapter {
        public void setPhotoData() {
            if (photoViews == null) {
                photoViews=new ArrayList<>();
                return;
            }
            photoViews.clear();
            for (int i = 0; i < articleList.size(); i++) {
                PhotoView iv = new PhotoView(getApplicationContext());
                picasso.load(DSRequest.SERVER_BASE1+articleList.get(i).getUrl())
                        .error(R.drawable.img_default)
                        .tag(getApplicationContext())
                        .into(iv);
                photoViews.add(iv);
            }
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return photoViews.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            PhotoView iv = photoViews.get(position);
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(photoViews.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }
}


